package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallWebProductMapper;
import org.linlinjava.litemall.db.domain.LitemallWebProduct;
import org.linlinjava.litemall.db.domain.LitemallWebProductExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallWebProductService {
    @Resource
    private LitemallWebProductMapper webProductMapper;
    private LitemallWebProduct.Column[] CHANNEL = {LitemallWebProduct.Column.id, LitemallWebProduct.Column.name};


    public LitemallWebProduct findById(Integer id) {
        return webProductMapper.selectByPrimaryKey(id);
    }

    public List<LitemallWebProduct> list(Integer id, String name, Integer page, Integer limit, String sort, String order) {
        List<LitemallWebProduct> productList = selectByExampleWithBLOBs(id, name, page, limit, sort, order);
        return productList;
    }

    public List<LitemallWebProduct> querySelective(Integer id, String name, Integer page, Integer size, String sort, String order) {
        LitemallWebProductExample example = new LitemallWebProductExample();
        LitemallWebProductExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return webProductMapper.selectByExample(example);
    }
    public List<LitemallWebProduct> selectByExampleWithBLOBs(Integer id, String name, Integer page, Integer size, String sort, String order) {
        LitemallWebProductExample example = new LitemallWebProductExample();
        LitemallWebProductExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return webProductMapper.selectByExampleWithBLOBs(example);
    }
    public int updateById(LitemallWebProduct product) {
        product.setUpdateTime(LocalDateTime.now());
        return webProductMapper.updateByPrimaryKeySelective(product);
    }

    public void deleteById(Integer id) {
        webProductMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallWebProduct product) {
        product.setAddTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());
        webProductMapper.insertSelective(product);
    }

    public List<LitemallWebProduct> queryChannel() {
        LitemallWebProductExample example = new LitemallWebProductExample();
        example.or().andDeletedEqualTo(false);
        return webProductMapper.selectByExampleSelective(example, CHANNEL);
    }
    public List<LitemallWebProduct> selectList() {
        LitemallWebProductExample example = new LitemallWebProductExample();
        example.or().andDeletedEqualTo(false);
        return webProductMapper.selectByExampleSelective(example);
    }
}
